﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nsf.Samples.RetryActivitySample.Infrastructure.Utils
{
    public class ExceptionHelper
    {
        public static String GetSimpleDescription(Exception ex)
        {
            var border = new String('=', 20);
            return String.Format("{0}{1}Exception Type:{1}{2}{1}Exception Message:{1}{3}{1}Stack Trace:{1}{4}{1}{0}", border, Environment.NewLine, ex.GetType().FullName, ex.Message, ex.StackTrace);
        }

        public static String GetSimpleStackDescription(Exception ex)
        {
            var border = new String('=', 20);
            return String.Format("{0}{1}Stack Trace:{1}{2}{1}{0}", border, Environment.NewLine, ex.StackTrace);
        }


        public static String GetInformation(Exception ex)
        {
            var sb = new StringBuilder(ex.Message);
            while (ex.InnerException != null)
            {
                sb.AppendLine(ex.InnerException.Message);
                ex = ex.InnerException;
            }
            return sb.ToString();
        }

        public static String GetFullInformation(Exception ex)
        {
            var sb = new StringBuilder();
            sb.AppendLine(GetSimpleDescription(ex));
            while (ex.InnerException != null)
            {
                sb.AppendLine(GetSimpleDescription(ex.InnerException));
                ex = ex.InnerException;
            }
            return sb.ToString();
        }

        public static String GetStackInformation(Exception ex)
        {
            var sb = new StringBuilder();
            sb.AppendLine(GetSimpleStackDescription(ex));
            while (ex.InnerException != null)
            {
                sb.AppendLine(GetSimpleStackDescription(ex.InnerException));
                ex = ex.InnerException;
            }
            return sb.ToString();
        }

    }
}
